package 人事管理系统.GUI;

import 人事管理系统.Connect.Department_Database;
import 人事管理系统.Tables.Department;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class dep_index {
    public dep_index(){
        JFrame jf = new JFrame("部门管理");
        jf.setBounds(350, 200, 800, 500);

        // 添加面板
        jf.setVisible(true);
        jf.setLayout(null);

        JPanel contentPane = new JPanel();
        jf.setContentPane(contentPane);
        //添加goodsNum查找框
        JTextField queryNum = new JTextField(20);
        queryNum.setText("该处输入待查找/删除/修改的部门编号：");

        queryNum.setBounds(100,20,165,25);
        contentPane.add(queryNum);
        JButton query=new JButton("查找");
        contentPane.add(query);
        query.setBackground(Color.PINK);
        JButton add=new JButton("增加");
        contentPane.add(add);
        add.setBackground(Color.PINK);
        JButton modify=new JButton("修改");
        contentPane.add(modify);
        modify.setBackground(Color.PINK);
        JButton delete=new JButton("删除");
        contentPane.add(delete);
        delete.setBackground(Color.PINK);
        JButton load=new JButton("加载");
        contentPane.add(load);
        load.setBackground(Color.PINK);

        JTextArea jta=new JTextArea("该处显示查找内容",1,30);
        jta.setLineWrap(true);    //设置文本域中的文本为自动换行
        jta.setForeground(Color.BLACK);    //设置组件的背景色
        jta.setFont(new Font("楷体",Font.BOLD,16));    //修改字体样式
        jta.setBackground(Color.pink);    //设置按钮背景色
        contentPane.add(jta);
        //文本框区域
        Object[][] tableDate=new Object[100][3];
        for(int i=0;i<20;i++)
        {
            tableDate[i][0]="";
            for(int j=1;j<3;j++)
            {
                tableDate[i][j]="";
            }
        }

        String[] name={"DNO","DNAME","manager"};
        JTable table=new JTable(tableDate,name);
        contentPane.add(new JScrollPane(table));

        //加载按钮监听事件
        load.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Department_Database goodsDao= new Department_Database();
                ArrayList<Department> list=new ArrayList<Department>();
                list=goodsDao.findAll();
                // DefaultTableModel model=(DefaultTableModel) table.getModel();    //获得表格模型
                int num=1;
                //先清空
                for(int i=0;i<100;i++)
                {
                    tableDate[i][0]="";
                    for(int j=1;j<3;j++)
                    {
                        tableDate[i][j]="";
                    }
                }
                //再读取数据库中数据
                for(Department g:list)
                {
                    tableDate[num][0]=g.getDno();
                    tableDate[num][1]=g.getDname();
                    tableDate[num][2]=g.getManager();
                    num++;
                }
                table.repaint();
            }
        });


        //查找监听事件
        query.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Department_Database department_database= new Department_Database();
                String dno=new String();
                dno=(queryNum.getText());
                Department department;
                department=department_database.findByDno(dno);
                jta.setText(department.getDno()+"\n"+department.getDname()+"\n"+department.getManager());
            }
        });

        //删除监听
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Department_Database department_database= new Department_Database();
                String dno=new String();
                dno=(queryNum.getText());
                int result=department_database.delete(dno);
            }
        });
        //修改事件
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Department_Database department_database= new Department_Database();
                String dno=new String();
                dno=(queryNum.getText());
                Department d=department_database.findByDno(dno);
                d.setDno(tableDate[0][0].toString());
                d.setDname(tableDate[0][1].toString());
                d.setManager(tableDate[0][2].toString());
                department_database.updata(d);

            }
        });


    }
}
